$(function() {
    initTreeTable();
});

function initTreeTable() {
    var setting = {
        id: 'deptId',
        code: 'deptId',
        url: ctx + 'dept/list',
        expandAll: true,
        expandColumn: "2",
        ajaxParams: {
            deptName: ""
        },
        columns: [{
                field: 'selectItem',
                checkbox: true
            },
            {
                title: '编号',
                field: 'deptId',
                width: '50px'
            },
            {
                title: '名称',
                field: 'deptName'
            },
            {
                title: '创建时间',
                field: 'createTime'
            }
        ]
    };
    $JuXue.initTreeTable('deptTable', setting);
}

function dept_search() {
    initTreeTable();
}

function deptRefresh() {
    dept_search();
}
/**删除**/
function deleteDepts() {
    var ids = $("#deptTable").bootstrapTreeTable("getSelections");
    var ids_arr = "";
    if (!ids.length) {$JuXue.n_warning("请勾选需要删除的部门！");return;}
    for (var i = 0; i < ids.length; i++) {
        ids_arr += ids[i].id;
        if (i !== (ids.length - 1)) ids_arr += ",";
    }
    $JuXue.confirm({
        text: "确定删除选中部门？",
        confirmButtonText: "确定删除"
    }, function() {
        $.post(ctx + 'dept/delete', { "ids": ids_arr }, function(r) {
            if (r.code === 0) {
                $JuXue.n_success(r.msg);
                deptRefresh();
            } else {
                $JuXue.n_danger(r.msg);
            }
        });
    });
}

function exportDeptExcel(){
	$.post(ctx+"dept/excel",$(".dept-table-form").serialize(),function(r){
		if (r.code === 0) {
			window.location.href = "file/download?fileName=" + r.msg + "&delete=" + true;
		} else {
			$JuXue.n_warning(r.msg);
		}
	});
}

/**增加部门**/
function addDepts() {
    $.post(ctx+"dept/addDialog",{},function (r) {$("#dept_info").empty().append(r).modal("show").draggable();});
}
/**编辑部门**/
function editDepts() {
    var selected = $("#deptTable").bootstrapTreeTable("getSelections");
    var selected_length = selected.length;
    if (!selected_length) {$JuXue.n_warning('请勾选需要修改的部门！');return;}
    if (selected_length > 1) {$JuXue.n_warning('一次只能修改一个部门！');return;}
    $.post(ctx+"dept/editDialog",{},function (r) {$("#dept_info").empty().append(r).modal("show").draggable();});
}
